package com.neusoft.elm.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.neusoft.elm.dao.FoodDao;
import com.neusoft.elm.po.Business;
import com.neusoft.elm.po.Food;
import com.neusoft.elm.util.DBUtil;

public class FoodDaoImpl implements FoodDao {

	Connection con = null;
	PreparedStatement pst = null;
	ResultSet rs = null;
	
	@Override
	public List<Food> listFoodByBusinessId(Integer businessId) {
		
		List<Food> list = new ArrayList<>();
		String sql = "SELECT * FROM food WHERE businessId = ? ";
		
		try {
			con = DBUtil.getConnection();
			pst = con.prepareStatement(sql);
			pst.setInt(1, businessId);
			rs = pst.executeQuery();
			
			while(rs.next()){
				Food food = new Food();
				food.setBusinessId(businessId);
				food.setFoodExplain(rs.getString("foodExplain"));
				food.setFoodId(rs.getInt("foodId"));
				food.setFoodName(rs.getString("foodName"));
				food.setFoodPrice(rs.getDouble("foodPrice"));
				list.add(food);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally{
			DBUtil.closeAll(rs, pst, con);
		}
		
		return list;
	}

	@Override
	public int saveFood(Food food) {
		int result = -1;
		String sql = "INSERT INTO food VALUES (null ,? ,? ,? ,?) ";
		
		
		try {
			con = DBUtil.getConnection();
			pst = con.prepareStatement(sql);
			pst.setString(1, food.getFoodName());
			pst.setString(2, food.getFoodExplain());
			pst.setDouble(3, food.getFoodPrice());
			pst.setInt(4,food.getBusinessId() );
			result = pst.executeUpdate();
			
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			DBUtil.closeAll(rs, pst, con);
		}
		
		return result;
	}

	@Override
	public int updateFood(Food food) {
		int result = -1;
	    String sql = "UPDATE food SET foodName = ?, foodExplain = ?, foodPrice = ? WHERE foodId = ?";
	    
	    try {
	        con = DBUtil.getConnection();

	        pst = con.prepareStatement(sql);
	        pst.setString(1, food.getFoodName());
	        pst.setString(2, food.getFoodExplain());
	        pst.setDouble(3, food.getFoodPrice());
//	        pst.setInt(4, food.getBusinessId());
	        pst.setInt(4, food.getFoodId());

	        result = pst.executeUpdate();
	        
	    } catch (SQLException e) {
	        e.printStackTrace();
	    } finally{
	        DBUtil.closeAll(rs, pst, con);
	    }
	    
	    return result;
	}

	@Override
	public Food getFoodByFoodId(Integer foodId) {
		String sql = "SELECT * FROM food WHERE foodId = ? ";
		Food food = new Food();
		try {
			con = DBUtil.getConnection();
			pst = con.prepareStatement(sql);
			pst.setInt(1, foodId);
			rs = pst.executeQuery();
			
			while(rs.next()){
				food.setBusinessId(rs.getInt("businessId"));
				food.setFoodExplain(rs.getString("foodExplain"));
				food.setFoodId(foodId);
				food.setFoodName(rs.getString("foodName"));
				food.setFoodPrice(rs.getDouble("foodPrice"));
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		} finally{
			DBUtil.closeAll(rs, pst, con);
		}
		
		return food;
	}

	@Override
	public int removeFood(Integer foodId) {
		int result = -1;
		String delFoodSql = "DELETE FROM food WHERE foodId = ? ";
		
		try {
			
			con = DBUtil.getConnection();
			
//			开启事务
			con.setAutoCommit(false);
			
//			删除食品信息
			pst = con.prepareStatement(delFoodSql);
			pst.setInt(1, foodId);
			result = pst.executeUpdate();

//			事务提交
			con.commit();
			
			
			
		} catch (SQLException e) {
			result = 0;
//			事务回滚
			try {
				con.rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
		} finally{
			DBUtil.closeAll(rs, pst, con);
		}
		
		return result;
	}

}
